package wpwork.mapper;

import org.apache.ibatis.annotations.*;
import wpwork.base.mapper.SuperMapper;
import wpwork.entity.KshGroupEntity;
import wpwork.model.GroupOrPersonDTO;
import wpwork.model.PersonByGroupDTO;

import java.util.List;
@Mapper
public interface KshGroupTableMapper extends SuperMapper<KshGroupEntity> {

    @Select("SELECT * FROM ksh_group_table WHERE f_parent_id = #{parentId} AND f_deleted = false ORDER BY f_sort_order, f_id")
    @Results(id = "groupResultMap", value = {
            @Result(property = "id", column = "f_id"),
            @Result(property = "name", column = "f_name"),
            @Result(property = "code", column = "f_code"),
            @Result(property = "parentId", column = "f_parent_id"),
            @Result(property = "level", column = "f_level"),
            @Result(property = "path", column = "f_path"),
            @Result(property = "description", column = "f_description"),
            @Result(property = "sortOrder", column = "f_sort_order"),
            @Result(property = "createdBy", column = "f_created_by"),
            @Result(property = "createdTime", column = "f_created_time"),
            @Result(property = "updatedBy", column = "f_updated_by"),
            @Result(property = "updatedTime", column = "f_updated_time"),
            @Result(property = "deleted", column = "f_deleted")
    })
    List<KshGroupEntity> selectByParentId(@Param("parentId") Long parentId);

    @Select("SELECT * FROM ksh_group_table WHERE f_path LIKE CONCAT(#{path}, '%') AND f_deleted = false ORDER BY f_sort_order, f_id")
    @ResultMap("groupResultMap")
    List<KshGroupEntity> selectChildrenByPath(@Param("path") String path);

    @Select("SELECT * FROM ksh_group_table WHERE f_deleted = false ORDER BY f_sort_order, f_id")
    @ResultMap("groupResultMap")
    List<KshGroupEntity> selectAllGroups();

    @Update("UPDATE ksh_group_table SET f_deleted = true WHERE f_id = #{id}")
    int logicDeleteById(@Param("id") Long id);

    @Select("SELECT 'group' AS grouptype, " +
            "       CAST(a.f_group_id AS VARCHAR(100)) AS groupId, " +
            "       d.f_name AS groupName, " +
            "       d.f_name AS groupRealname, " +
            "       d.f_name AS groupIcon " +
            "FROM ( " +
            "    SELECT f_group_id " +
            "    FROM ksh_user_group_relation " +
            "    GROUP BY f_group_id " +
            "    HAVING COUNT(*) > 1 " +
            ") a " +
            "INNER JOIN ksh_group_table d ON a.f_group_id = d.f_id " +

            "UNION ALL " +

            "SELECT 'person' AS grouptype, " +
            "       a.f_user_id AS groupId, " +
            "       d.f_account AS groupName, " +
            "       d.f_real_name AS groupRealname, " +
            "       d.f_head_icon AS groupIcon " +
            "FROM ksh_user_group_relation a " +
            "INNER JOIN base_user d ON a.f_user_id = d.f_id")
    List<GroupOrPersonDTO> selectGroupAndPersonList();

    @Select("SELECT DISTINCT 'person' AS grouptype, " +
            "       a.f_user_id AS groupId, " +
            "       d.f_account AS groupName, " +
            "       d.f_real_name AS groupRealname, " +
            "       d.f_head_icon AS groupIcon, " +
            "       CAST(a.f_group_id AS VARCHAR(100)) AS groupParentId, " +
            "       g.f_name AS groupParentName " +
            "FROM ksh_user_group_relation a " +
            "INNER JOIN base_user d ON a.f_user_id = d.f_id " +
            "INNER JOIN ksh_group_table g ON a.f_group_id = g.f_id")
    List<PersonByGroupDTO> selectPersonAndGroupName();


}
